<%@page import="com.amphora.web.actions.CalendarAction"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.amphora.services.application.BusinessServices" %>
<%@page import="com.amphora.services.application.impl.BusinessServicesImpl" %>
<%@page import="com.amphora.services.application.ClientServices" %>
<%@page import="com.amphora.services.application.impl.ClientServiceImpl" %>
<%@page import="com.amphora.util.repository.JPAUtil" %>
<%@page import="com.amphora.model.TreatmentType" %>
<%@page import="com.amphora.model.CalendarEvent" %>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="java.util.List"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.sql.Time"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%
BusinessServices treatmentController = new BusinessServicesImpl();
List<TreatmentType> allTreatments = treatmentController.viewAllTreatmentType();

if(request.getParameter("getTreatments")!=null){
	JSONArray jsonTreatments = new JSONArray();
	for(TreatmentType t:allTreatments){
		JSONObject js = new JSONObject();
		js.put("id", t.getId());
		js.put("name", t.getName());
		js.put("description", t.getDescription());
		js.put("price", t.getPrice());
		js.put("duration", t.getTreatmentDuration());
		js.put("category", t.getCategory().getName());
		jsonTreatments.add(js);
	}
	out.print(jsonTreatments.toString());
}

if(request.getParameter("checkTime")!=null){
	if(request.getParameter("items")!=null&&!request.getParameter("items").equals("")){
		ClientServices clientServices = new ClientServiceImpl();
		String itemIds = request.getParameter("items");
		String startTimeSelected = request.getParameter("appointmentDate");
		
		List<TreatmentType> tt = new LinkedList<TreatmentType>();
		String[] tIDS = itemIds.split(",");
		
		for(int i = 0;i < tIDS.length;i++){
			TreatmentType t = JPAUtil.findById(TreatmentType.class, new Long(tIDS[i]));
			tt.add(t);
		}
		
		Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTimeSelected);
		Calendar appointmentMaybeDate = Calendar.getInstance();
		appointmentMaybeDate.setTime(d);
		
		Long clientID = new Long(request.getSession().getAttribute("loginID").toString());
		String isValid = clientServices.createNewBooking(appointmentMaybeDate, clientID, tt);
		if(isValid.indexOf("Error") == -1)
			out.print("success");
		else
			out.print(isValid);
	}
	
}

if(request.getParameter("timeList")!=null){
	Time dayStart = new Time(9,0,0);
	JSONArray jsonTimePeriods = new JSONArray();
	for(int i = 0;i < 18;i++){
		JSONObject js = new JSONObject();
		js.put("time", dayStart.toString());
		jsonTimePeriods.add(js);
		
		dayStart.setMinutes(dayStart.getMinutes()+30);
	}
	
	out.print(jsonTimePeriods);
}

if(request.getParameter("populateCalendarEvents")!=null){
	JSONArray jsonCalendarEvents = new JSONArray();
	
	List<CalendarEvent> ce = new LinkedList<CalendarEvent>();
	ce = treatmentController.getAllCalendarEventForMonthYear();	
	
	SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	if(ce!=null&&ce.size()>0){
		for(CalendarEvent c:ce){
			JSONObject event = new JSONObject();
			event.put("id",c.getId());
			event.put("title", c.getTitle());
			event.put("start", format1.format(c.getStartTime().getTime()));
			event.put("end", format1.format(c.getEndTime().getTime()));
			jsonCalendarEvents.add(event);
		}
	}
	out.print(jsonCalendarEvents);
}
%>